package day2.listener;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener
public class MyListener implements ServletContextListener,HttpSessionListener,ServletRequestListener,HttpSessionAttributeListener {
    int count;
    ServletContext app;
    @Override
    public void contextInitialized(ServletContextEvent event) {
        System.out.println("application被创建了。");
        app=event.getServletContext();
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("application被销毁了。。。");
    }

    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
        System.out.println("request被销毁了");
    }

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {
        System.out.println("request被创建了");
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent e) {
        System.out.println("session中添加了数据e");
        System.out.println(e.getSession());
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("session中数据被删除了");
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("session中数据被替换了");
    }

    @Override
    public void sessionCreated(HttpSessionEvent event) {
        System.out.println("session被创建了");
        app.setAttribute("count",count++);


    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        System.out.println("session被销毁了。。。");
    }
}
